<?php
declare (strict_types=1);

namespace app\admin\service\site;

use app\admin\logic\system\Config as SystemConfig;
use app\common\logic\Attach;
use app\common\service\BaseService;
use app\school\model\school\admin\School;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\page\PageForm;
use think\Request;
use think\Response;

/**
 * 站点信息配置
 */
class Config
{
    /**
     * 站点基础信息配置
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-03
     *
     * @param Request $request
     * @return Response [type]                         [description]
     */
    public function info(Request $request): Response
    {
        $key = 'site.info';
        // 配置字段
        $fields = [
            'school_code'     => FormItem::make('word')->title('机构编码'),
            'title'           => FormItem::make()->title('网站名称')->required(),
            'admin_logo'      => FormItem::make('custom')->title('后台管理中心LOGO')->limit(1)->help('建议尺寸  150px*40px'),
            'pc_logo'         => FormItem::make('custom')->title('PC站点LOGO')->limit(1)->help('建议尺寸  150px*40px'),
            'h5_logo'         => FormItem::make('custom')->title('H5站点LOGO')->limit(1)->help('建议高度40px'),
            'ico'             => FormItem::make('custom')->title('ICO图标')->limit(1)->help('建议尺寸 28px*28px'),
            'official_qr'     => FormItem::make('custom')->title('公众号二维码')->limit(1)->help('建议尺寸 132px*132px'),
            'seo_keyword'     => FormItem::make('textarea')->title('seo关键词'),
            'seo_description' => FormItem::make('textarea')->title('seo描述'),
            'online_consult'  => FormItem::make()->title('在线咨询')->help(' 可以填写一个QQ号码或其他客服链接'),
            'copyright'       => FormItem::make()->title('版权信息'),
            'icp'             => FormItem::make()->title('icp备案号'),
            'portal_code'     => FormItem::make()->title('公安备案号'),
            'tel'             => FormItem::make()->title('咨询电话'),
            'time'            => FormItem::make()->title('服务时间'),
            'stats_code'      => FormItem::make('textarea')->title('第三方统计代码')->help('只需配置第三方的统计链接地址，如：https://hm.baidu.com/hm.js?xxx'),

        ];

        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $config          = SystemConfig::get($key, [], $request->mhm_id);
        $config['__key'] = $key;
        // 后台LOGO
        if ($config['admin_logo'] ?? false) {
            $config['admin_logo_url'] = app(Attach::class)->where('id', $config['admin_logo'])->findOrEmpty()->getAttr('fileurl');
        }
        // pc_logo
        if ($config['pc_logo'] ?? false) {
            $config['pc_logo_url'] = app(Attach::class)->where('id', $config['pc_logo'])->findOrEmpty()->getAttr('fileurl');
        }
        // h5_logo
        if ($config['h5_logo'] ?? false) {
            $config['h5_logo_url'] = app(Attach::class)->where('id', $config['h5_logo'])->findOrEmpty()->getAttr('fileurl');
        }
        // pc_logo
        if ($config['ico'] ?? false) {
            $config['ico_url'] = app(Attach::class)->where('id', $config['ico'])->findOrEmpty()->getAttr('fileurl');
        }
        // 公众号二维码
        if ($config['official_qr'] ?? false) {
            $config['official_qr_url'] = app(Attach::class)->where('id', $config['official_qr'])->findOrEmpty()->getAttr('fileurl');
        }
        // 机构编码
        $config['school_code'] = app(School::class)->where('id', request()->mhm_id)->value('school_code');;
        $form->datas = $config;

        return $form->send();
    }

    /**
     * 机构独立APP开关
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-30
     *
     * @return Response [type]                         [description]
     */
    public function schoolApp(): Response
    {
        $fields        = [
            'school_app' => FormItem::make('SlideSwitch')->title('机构独立APP')->activeValue()->inactiveValue(0)->help('开启后，APP端将展示机构APP'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'system.school.app';
        $config          = SystemConfig::get($key, ['school_app' => 0]);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();
    }

    /**
     * 获取配置的后台LOGO
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-30
     *
     * @return Response [type]                         [description]
     */
    public function adminLogo(): Response
    {
        $info = SystemConfig::get('site.info', [], request()->mhm_id);
        $url  = '';
        if ($info['admin_logo'] ?? false) {
            $url = app(Attach::class)->where('id', $info['admin_logo'])->findOrEmpty()->getAttr('fileurl');
        }

        $ico = '';
        if ($info['ico'] ?? false) {
            $ico = app(Attach::class)->where('id', $info['ico'])->findOrEmpty()->getAttr('fileurl');
        }

        $pcLogo = '';
        if ($info['pc_logo'] ?? false) {
            $pcLogo = app(Attach::class)->where('id', $info['pc_logo'])->findOrEmpty()->getAttr('fileurl');
        }

        $h5Logo = '';
        if ($info['h5_logo'] ?? false) {
            $h5Logo = app(Attach::class)->where('id', $info['h5_logo'])->findOrEmpty()->getAttr('fileurl');
        }

        return BaseService::parseToData([
            'logo'        => $url,
            'ico_url'     => $ico,
            'pc_logo_url' => $pcLogo,
            'h5_logo_url' => $h5Logo
        ], 1);
    }
}
